#include "tires.h"
#include <stdbool.h>
#include "lvgl/lvgl.h"
#include "control.h"
#include <stdio.h>
#include "ui/ui.h"
#include <stdlib.h>

// 轮胎UI对象数组
static TireUIObjects tire_ui[4];

// 轮胎状态数组
static TireStatus tire_status[4];

// 异步更新参数结构体
typedef struct {
    TirePosition pos;
    int pressure;
    int temp;
} TireUpdateArgs;

// 初始化轮胎UI对象
void init_tire_ui_objects() {
    // 前左轮
    tire_ui[TIRE_FRONT_LEFT] = (TireUIObjects){
        .ty_box = ui_tybox1,
        .jg_box = ui_jgbox1,
        .ty_label = ui_ty1,
        .temp_label = ui_tw1,
        .ty_status_label = ui_tyxs1,
        .temp_status_label = ui_twxs1
    };
    
    // 前右轮
    tire_ui[TIRE_FRONT_RIGHT] = (TireUIObjects){
        .ty_box = ui_tybox2,
        .jg_box = ui_jgbox2,
        .ty_label = ui_ty2,
        .temp_label = ui_tw2,
        .ty_status_label = ui_tyxs2,
        .temp_status_label = ui_twxs2
    };
    
    // 后左轮
    tire_ui[TIRE_REAR_LEFT] = (TireUIObjects){
        .ty_box = ui_tybox3,
        .jg_box = ui_jgbox3,
        .ty_label = ui_ty3,
        .temp_label = ui_tw3,
        .ty_status_label = ui_tyxs3,
        .temp_status_label = ui_twxs3
    };
    
    // 后右轮
    tire_ui[TIRE_REAR_RIGHT] = (TireUIObjects){
        .ty_box = ui_tybox4,
        .jg_box = ui_jgbox4,
        .ty_label = ui_ty4,
        .temp_label = ui_tw4,
        .ty_status_label = ui_tyxs4,
        .temp_status_label = ui_twxs4
    };
}

int get_tire_pressure(TirePosition position) {
    if (position < 0 || position > 3) return 0;
    return tire_status[position].pressure;
}

int get_tire_temperature(TirePosition position) {
    if (position < 0 || position > 3) return 0;
    return tire_status[position].temperature;
}

void update_tire_status(TirePosition position, int pressure, int temperature) {
    // 确保位置有效
    if (position < 0 || position > 3) return;

    // 添加边界检查
    if (position < 0 || position > 3) {
        printf("错误：无效的轮胎位置！\n");
        return;
    }
    
    // 检查UI对象是否初始化
    if (!tire_ui[position].ty_label || !tire_ui[position].temp_label) {
        printf("错误：轮胎UI对象未正确初始化！\n");
        return;
    }
    
    // 更新状态
    tire_status[position].pressure = pressure;
    tire_status[position].temperature = temperature;
    
    // 检查是否在正常范围
    bool pressure_ok = (pressure >= NORMAL_PRESSURE_MIN && pressure <= NORMAL_PRESSURE_MAX);
    bool temp_ok = (temperature >= NORMAL_TEMP_MIN && temperature <= NORMAL_TEMP_MAX);
    tire_status[position].is_normal = pressure_ok && temp_ok;
    
    // 获取对应UI对象
    TireUIObjects *ui = &tire_ui[position];
    
    // 更新胎压显示
    char buffer[20];
    snprintf(buffer, sizeof(buffer), "%dkPa", pressure);
    lv_label_set_text(ui->ty_label, buffer);
    
    // 更新温度显示
    snprintf(buffer, sizeof(buffer), "%d°C", temperature);
    lv_label_set_text(ui->temp_label, buffer);
    
    // 更新状态标签
    if (pressure_ok) {
        lv_label_set_text(ui->ty_status_label, "胎压正常");
    } else if (pressure < NORMAL_PRESSURE_MIN) {
        lv_label_set_text(ui->ty_status_label, "胎压过低");
    } else {
        lv_label_set_text(ui->ty_status_label, "胎压过高");
    }
    
    if (temp_ok) {
        lv_label_set_text(ui->temp_status_label, "温度正常");
    } else if (temperature < NORMAL_TEMP_MIN) {
        lv_label_set_text(ui->temp_status_label, "温度过低");
    } else {
        lv_label_set_text(ui->temp_status_label, "温度过高");
    }
    
    // 更新显示状态
    if (tire_status[position].is_normal) {
        lv_obj_clear_flag(ui->ty_box, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(ui->jg_box, LV_OBJ_FLAG_HIDDEN);
    } else {
        lv_obj_add_flag(ui->ty_box, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(ui->jg_box, LV_OBJ_FLAG_HIDDEN);
    }
}

void check_all_tires() {
    bool all_normal = true;
    
    // 检查车辆整体状态
    for (int i = 0; i < 4; i++) {
        if (!tire_status[i].is_normal) {
            all_normal = false;
            break;
        }
    }
    
    // 更新车辆状态图标
    if (all_normal) {
        lv_obj_clear_flag(ui_carjcon, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(ui_carwton, LV_OBJ_FLAG_HIDDEN);
    } else {
        lv_obj_clear_flag(ui_carwton, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(ui_carjcon, LV_OBJ_FLAG_HIDDEN);
    }
}

/* 异步更新回调函数 */
void update_tire_status_async(void *arg) {
    TireUpdateArgs *args = (TireUpdateArgs *)arg;
    if (args) {
        update_tire_status(args->pos, args->pressure, args->temp);
        check_all_tires();
        free(args); // 释放内存
    }
}

/* 设置轮胎胎压 */
void set_tire_pressure(TirePosition position, int pressure) {
    int temp = get_tire_temperature(position);
    TireUpdateArgs *args = malloc(sizeof(TireUpdateArgs));
    if (args) {
        args->pos = position;
        args->pressure = pressure;
        args->temp = temp;
        safe_async_call(update_tire_status_async, args);
    }
}

/* 设置轮胎温度 */
void set_tire_temperature(TirePosition position, int temperature) {
    int pressure = get_tire_pressure(position);
    TireUpdateArgs *args = malloc(sizeof(TireUpdateArgs));
    if (args) {
        args->pos = position;
        args->pressure = pressure;
        args->temp = temperature;
        safe_async_call(update_tire_status_async, args);
    }
}

/* 同时设置轮胎胎压和温度 */
void set_tire_pressure_and_temperature(TirePosition position, int pressure, int temperature) {
    TireUpdateArgs *args = malloc(sizeof(TireUpdateArgs));
    if (args) {
        args->pos = position;
        args->pressure = pressure;
        args->temp = temperature;
        safe_async_call(update_tire_status_async, args);
    }
}